# 参考 https://www.cnblogs.com/shenh/p/9090586.html
# 异步
# https://www.cnblogs.com/zycorn/p/10554206.html
# https://blog.csdn.net/Yooyi_xin/article/details/119698766?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-2.no_search_link

import asyncio
from aiohttp import ClientSession, TCPConnector

url = "https://www.baidu.com/{}"
tasks = []

async def hello(url, headers,sem ):
    try:
        async with sem:
            async with ClientSession(headers={'User-Agent': headers},
                                     connector=TCPConnector(limit=64, verify_ssl=False)
                                     ) as session:
                async with session.get(url,  allow_redirects=False) as response:
                        print(response.status)
                        print(url)
                        host = url.split('/')[2].split(":")[0]
                        print("respose result",await response.read())
                        return await response.read()
    except Exception as e:
        print("erro req",e)


def run():
    for i in range(5):
        task = asyncio.ensure_future(hello(url.format(i),"dd"))
        tasks.append(task)
    result = loop.run_until_complete(asyncio.gather(*tasks))
    print(result)



def get_url():
    urls = []
    uris = [
        "/api.html",
    ]

def quickly():
    urls = ['https://www.baidu.com/s?','https://zhuanlan.zhihu.com/p/35557703']
    print('---start url----')
    print(urls)
    print('----end url---')
    sem = asyncio.Semaphore(100)  # limit await 100 signal
    for url in urls:
        host = url.split('/')[2].split(":")[0]
        task = asyncio.ensure_future(hello(url,host, sem))
        tasks.append(task)
    result = loop.run_until_complete(asyncio.gather(*tasks))




if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    quickly()
    # run()